#include "stdafx.h"
#include "detail/bmock_logfile.hpp"
#include <iostream>

namespace bmock {
	bmock_logfile* bmock_logfile::instance_ = NULL;
	bmock_logfile::killer bmock_logfile::killer_instance_;

	bmock_logfile::bmock_logfile(const std::string& filename, bool log_to_stdout) {
		try {
			ofile_.open(filename);
		} catch (...) {}
		
		log_to_stdout_ = log_to_stdout;
	}

	void bmock_logfile::add_to_log(const std::string& str)
	{
		if (ofile_.is_open())
		{
			ofile_ << str << std::endl;
			ofile_.flush();
		}

		if (log_to_stdout_)
			std::cout << str << std::endl;

	}

	bmock_logfile::~bmock_logfile() {
		if (ofile_.is_open())
			ofile_.close();
	}

	bmock_logfile::killer::~killer()
	{
		if (bmock_logfile::instance_)
			delete bmock_logfile::instance_;

		bmock_logfile::instance_ = NULL;
	}
}